#include "../include/trips.h"
#include "../include/passenger.h"
#include "../include/longin.h"

//创建头节点
TRAIN *tripsHead(TRAIN *head)
{
    head = (TRAIN *)malloc(sizeof(TRAIN));
    if(head == NULL)
    {
        perror("malloc head");
        exit(1);//申请空间失败
    }
    memset(head, 0, sizeof(TRAIN));

    return head;
}

//查看车次
void print(TRAIN *head)
{
    assert(head);
    TRAIN *end = head->next;
    int i = 1;
    printf(" %s --%s--    %s\t %s----%s  %s\n","序列", "车次", "发车时间", "起点", "终点", "余票");
   
    while(end != NULL)
    {
        printf(" %2d   --%s-- [%d-%d-%d %d:%d] %s----%s   %d\n", i++, end->tripsan.trip, 
            end->tripsan.time.tm_year, end->tripsan.time.tm_mon + 1, end->tripsan.time.tm_mday, 
            end->tripsan.time.tm_hour, end->tripsan.time.tm_min,
            end->tripsan.start, end->tripsan.destination, end->tripsan.passengernum);
        end = end->next;
    }
}

//修改车次
TRAIN *changeTrips(TRAIN *head, ADM *head1)
{
    assert(head);
    TRAIN *end = head->next;
    char str[20] = {0};
    int count = 0;
    int flg = 0;
    printf("请输入需要更改的车次：");
    scanf("%s", str);
    while(end != NULL)
    {
        if(!strcmp(end->tripsan.trip, str))
        {
            int num = 1;
            while(num)
            {
                printf("请选择想修改的信息：\n");
                printf("1、发车时间\n");
                printf("2、发车起点\n");
                printf("3、发车终点\n");
                printf("4、载客数量\n");
                printf("0、退出\n");
                while(1)
                {
                    printf("请选择：");
                    count = scanf("%d", &num);
                    if(count == 0)
                    {
                        getchar();
                        printf("输入有误！\n");
                        printf("请重新选择！\n");
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                switch(num)
                {
                    case 0: num = 0;
                            break;
                    case 1: printf("例如：[2024 12 10 12 16 12] \n");
                            printf("发车时间：");
                            scanf("%d%d%d%d%d", &end->tripsan.time.tm_year, &end->tripsan.time.tm_mon, 
                                &end->tripsan.time.tm_mday,&end->tripsan.time.tm_hour, 
                                &end->tripsan.time.tm_min);
                            end->tripsan.time.tm_mon--;
                            printf("更改信息成功！\n");
                            logMessage(LOG_INFO, "admChangeTrips %s", head1->adm.name);
                            break;
                    case 2: printf("起点：");
                            scanf("%s", end->tripsan.start);
                            printf("更改信息成功！\n");
                            logMessage(LOG_INFO, "admChangeTrips %s", head1->adm.name);
                            break;
                    case 3: printf("终点：");
                            scanf("%s", end->tripsan.destination);
                            printf("更改信息成功！\n");
                            logMessage(LOG_INFO, "admChangeTrips %s", head1->adm.name);
                            break;
                    case 4: printf("载客数：");
                            count = scanf("%d", &end->tripsan.passengernum);
                            if(count == 0)
                            {
                                getchar();
                                printf("输入有误！\n");
                                printf("请重新选择！\n");
                                continue;
                            }
                            printf("更改信息成功！\n");
                            logMessage(LOG_INFO, "admChangeTrips %s", head1->adm.name);
                            break;
                    default:
                            printf("没有这个选择！\n");
                            printf("请重新选择：");
                            break;
                }
            } 
            break;              
        }
        end = end->next;
    }
    if(end == NULL)
    {
        printf("没有该车次的列车！\n");
        printf("更改失败！\n");
        logMessage(LOG_INFO, "No this Trips");
    }

    return head;
}

//停运车次
TRAIN *stopTrips(TRAIN *head, ADM *head1)
{
    assert(head);
    TRAIN *end = head->next;
    char str[20] = {0};

    printf("请输入需要停运得车次：");
    scanf("%s", str);

    //防止只有一个节点
    TRAIN *tmp = head;
    while(end != NULL)
    {
        if(!strcmp(end->tripsan.trip, str))
        {
            //把删除节点的指针域给前一个节点的指针域
            tmp->next = end->next;
            free(end);
            end = NULL;
            printf("该班次停运成功！\n");
            logMessage(LOG_INFO, "admStopTrips %s", head1->adm.name);

            return head;
        }
        //找到删除的前一个节点
        tmp = end;
        end = end->next;
    }
   
    if(end == NULL)
    {
        printf("没有查询到该班次！\n");
        printf("停运失败！\n");
        logMessage(LOG_ERROR, "No this Trips!");

        return head;
    } 
}

//增加车次
TRAIN *addtrips(TRAIN *head, ADM *head1)
{
    assert(head);
    char str[20] = {0};
    printf("新增车次：");
    scanf("%s", str);

    TRAIN *end = head->next;
    while(end != NULL)
    {
        if(!strcmp(str, end->tripsan.trip))
        {
            printf("该班次列车已存在！\n");
            logMessage(LOG_WARNING, "AddTrips  already exists!");

            break;
        }
        end = end->next;
    }
    
    if(end == NULL)
    {
        //创建新的节点并赋值
        TRAIN *new = (TRAIN *)malloc(sizeof(TRAIN));
        if(new == NULL)
        {
            perror("malloc new");
            exit(1);
        }
        memset(new, 0, sizeof(TRAIN));

        strcpy(new->tripsan.trip, str);
        printf("例如：[2024 12 10 12 16 ] \n");
        printf("新增车次的发车时间：");
        scanf("%d%d%d%d%d", &new->tripsan.time.tm_year, &new->tripsan.time.tm_mon,
        &new->tripsan.time.tm_mday,&new->tripsan.time.tm_hour, 
        &new->tripsan.time.tm_min);

        new->tripsan.time.tm_mon--;
        printf("新增车次起点：");
        scanf("%s", new->tripsan.start);

        printf("新增车次终点：");
        scanf("%s", new->tripsan.destination);

        int count = 0;
        printf("新增车次载客数：");
        while(1)
        {
            count = scanf("%d", &new->tripsan.passengernum);
            if(count == 0)
            {
                getchar();
                printf("输入有误！\n");
                printf("请重新选择！\n");
                continue;
            }
            else
            {
                break;
            }
        }

        //尾插，遍历到链表末尾
        TRAIN *end = head;
        while(end->next != NULL)
        {
            end = end->next;
        }
        end->next = new;
        new->next = NULL;

        printf("增加成功！\n");
        logMessage(LOG_INFO, "admAddTrips %s", head1->adm.name);
    }
 
    return head;
}

//查询车次
void findTrips(TRAIN *head)
{
    assert(head);
    TRAIN *end = head->next;
    struct tm tim = {0};
    char location[20] = {0};
    char trips[20] = {0};
    int num = 0;
    int flg = 0;
    int count = 0;
    printf("查找方式：\n1、按时间查找\n2、按地点查找\n3、按车次查找\n");
    while(1)
    {
        printf("请选择查找方式：");
        count = scanf("%d", &num);
        if(count == 0)
        {
            getchar();
            printf("输入有误！\n");
            printf("请重新选择！\n");
            continue;
        }
        else
        {
            break;
        }
    }
    
    switch(num)
    {
        case 1:
            printf("例如：[2014 10 23 12 00 ]\n");
            printf("请输入时间：");
            scanf("%d%d%d%d%d", &tim.tm_year, &tim.tm_mon, &tim.tm_mday, &tim.tm_hour, &tim.tm_min);
            tim.tm_mon--;
            printf(" --%s--    %s\t    %s----%s  %s\n", "车次", "发车时间", "起点", "终点", "余票");
            time_t time2 = mktime(&tim);
            while(end != NULL)
            {
                time_t time1 = mktime(&end->tripsan.time);   
                if(time2 <= time1)
                {
                    printf(" --%s--  [%d-%d-%d %d:%d]\t %s----%s  %d\n", end->tripsan.trip, end->tripsan.time.tm_year,
                            end->tripsan.time.tm_mon + 1, end->tripsan.time.tm_mday, 
                            end->tripsan.time.tm_hour,end->tripsan.time.tm_min,end->tripsan.start, 
                            end->tripsan.destination, end->tripsan.passengernum);
                    flg++;
                }
                end = end->next;
            }
            
            if(flg == 0)
            {
                printf("  --%s--      %s        %s------%s     %d\n", "0", "0", "0", "0", 0);
                printf("此时间没有相关列车！\n");
                logMessage(LOG_WARNING, "No this time Trips!");
            }
            break;
        case 2: 
            printf("例如：西安\n");
            printf("请输入地址：");
            scanf("%s", location);
            printf(" --%s--      %s\t    %s----%s  %s\n", "车次", "发车时间", "起点", "终点", "余票");
            while(end != NULL)
            {
                if((!strcmp(end->tripsan.start, location)) || (!strcmp(end->tripsan.destination, location)))
                {
                    printf(" --%s--  [%d-%d-%d %d:%d]\t %s----%s  %d\n", end->tripsan.trip, end->tripsan.time.tm_year,
                        end->tripsan.time.tm_mon + 1, end->tripsan.time.tm_mday, 
                        end->tripsan.time.tm_hour, end->tripsan.time.tm_min,
                        end->tripsan.start, end->tripsan.destination, end->tripsan.passengernum);
                    flg++;
                }
                end = end->next;
            }
           
            if(flg == 0)
            {
                printf("  --%s--    %s\t    %s------%s     %d\n", "0", "0", "0", "0", 0);
                printf("此地址没有相关列车！\n");
                logMessage(LOG_WARNING, "No this locet Trips!");
            }

            break;
        case 3: 
            printf("例如：K129\n");
            printf("请输入车次：");
            scanf("%s", trips);
            printf(" --%s--        %s\t    %s----%s  %s\n", "车次", "发车时间", "起点", "终点", "余票");
            while(end != NULL)
            {
                if((!strcmp(end->tripsan.trip, trips)))
                {
                    printf(" --%s--  [%d-%d-%d %d:%d]     %s----%s  %d\n", end->tripsan.trip,
                        end->tripsan.time.tm_year,end->tripsan.time.tm_mon + 1, 
                        end->tripsan.time.tm_mday, end->tripsan.time.tm_hour, end->tripsan.time.tm_min,
                        end->tripsan.start, end->tripsan.destination, end->tripsan.passengernum);
                    flg++;
                }
                end = end->next;
            }
           
            if(flg == 0)
            {
                printf("  --%s--    %s\t    %s------%s     %d\n", "0", "0", "0", "0", 0);
                printf("没有此车次的列车！\n");
                logMessage(LOG_WARNING, "No this  Trips!");
            }
            break;
        default:
            printf("没有这种查找方式！\n");
            logMessage(LOG_WARNING, "No this findway!");
            break;
    }
}

//释放链表
void freetrips(TRAIN *head)
{
    TRAIN *end = NULL;
    end = head;
    if(end == NULL)
    {
        printf("link is empty!\n");
    }
    else
    {
        while(head != NULL)
        {
            head = head->next;
            free(end);
            end = head;
        }
    }
}
